/*
 * @Author: jiangjinchi 1468922694@qq.com
 * @Date: 2023-10-31 08:40:35
 * @LastEditors: jiangjinchi 1468922694@qq.com
 * @LastEditTime: 2024-02-04 09:12:42
 * @FilePath: \vue-admin-template\src\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: jiangjinchi 1468922694@qq.com
 * @Date: 2023-10-31 08:40:35
 * @LastEditors: jiangjinchi 1468922694@qq.com
 * @LastEditTime: 2023-11-08 10:13:25
 * @FilePath: \vue-admin-template\src\store\modules\user.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { login, logout, getInfo, getMenus } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import Cookies from 'js-cookie'
const getDefaultState = () => {
  return {
    name: '',
    avatar: '',
    menus: [],
    info: null,
    dialogPassword: false,
    passwordId: '',
    heightSize: '',
    fullScreen: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_PASSORD: (state, dialogPassword) => {
    state.dialogPassword = dialogPassword
  },
  SET_PASSORDID: (state, passwordId) => {
    state.passwordId = passwordId
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_MENUS: (state, menus) => { //新增
    state.menus = menus
  },
  SET_INFO: (state, info) => { //新增
    state.info = info
  },
  SET_PAGEHEIGHT: (state, size) => { //新增
    state.heightSize = size
  },
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response;
        Cookies.set('tokenNameEqu', data.tokenName);
        Cookies.set('tokenValueEqu', data.tokenValue);
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response;
        const { user } = data
        commit('SET_NAME', user.name)
        commit('SET_INFO', user)
        
        let arr  = data.menu.filter(item => item.children.length !== 0)
        arr.forEach(item => {

          if (item.children.length == 1) {
            delete item.name
          }
          if (item.children.length > 1) {
            item.children.map(item1 => {
              delete item1.children
            })
          }
        })
        const menus = arr
        menus.push({
          path: '/404',
          component: "404",
          hidden: true
        })
        commit('SET_MENUS', menus);

        resolve(data)


      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        Cookies.remove('tokenValueEqu');
        Cookies.remove('tokenNameEqu');
        resetRouter();
        commit('RESET_STATE');
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      Cookies.remove('tokenValueEqu');
      Cookies.remove('tokenNameEqu');
      resetRouter();
      commit('RESET_STATE');
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

